using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using CommunityToolkit.Mvvm.ComponentModel;
using torch.ui.Models;

namespace torch.ui.ViewModels;

public partial class HomeViewModel : ViewModelBase2
{
    [ObservableProperty]
    private string _userName = "张小明";

    [ObservableProperty]
    private int _pendingUpdates = 3;

    [ObservableProperty]
    private ObservableCollection<SoftwareItemModel> _purchasedSoftware = new();

    [ObservableProperty]
    private ObservableCollection<RecommendationModel> _recommendations = new();

    [ObservableProperty]
    private ObservableCollection<ActivityItemModel> _recentActivities = new();

    public string WelcomeTitle => $"欢迎回来，{UserName}";

    public string WelcomeSubtitle
    {
        get
        {
            var culture = CultureInfo.GetCultureInfo("zh-CN");
            var dayOfWeek = DateTime.Now.ToString("dddd", culture);
            return $"今天是{dayOfWeek}，有 {PendingUpdates} 个新的软件更新";
        }
    }

    public HomeViewModel()
    {
        LoadMockData();
    }

    partial void OnUserNameChanged(string value) => OnPropertyChanged(nameof(WelcomeTitle));

    partial void OnPendingUpdatesChanged(int value) => OnPropertyChanged(nameof(WelcomeSubtitle));

    public void ApplyDashboardData(DashboardData data)
    {
        UserName = string.IsNullOrWhiteSpace(data.UserName) ? UserName : data.UserName;
        PendingUpdates = data.PendingUpdates;

        ReplaceCollection(PurchasedSoftware, data.PurchasedSoftware);
        ReplaceCollection(Recommendations, data.Recommendations);
        ReplaceCollection(RecentActivities, data.RecentActivities);
    }

    private void LoadMockData()
    {
        PurchasedSoftware = new ObservableCollection<SoftwareItemModel>
        {
            new()
            {
                IconGlyph = "\uf15c",
                IconBackground = "#E8F2FF",
                Title = "文档编辑器",
                Description = "专业文档处理工具",
                StatusBadge = "已更新",
                StatusBadgeBackground = "#E6F7EF",
                StatusBadgeForeground = "#22C55E",
                ActionText = "启动",
                IsFavorite = true
            },
            new()
            {
                IconGlyph = "\uf1fc",
                IconBackground = "#F4EBFF",
                Title = "图像设计",
                Description = "专业图像编辑软件",
                StatusBadge = "可更新",
                StatusBadgeBackground = "#FFF4E5",
                StatusBadgeForeground = "#F97316",
                ActionText = "更新",
                IsFavorite = false
            },
            new()
            {
                IconGlyph = "\uf121",
                IconBackground = "#E8FFF4",
                Title = "代码编辑器",
                Description = "程序员必备工具",
                StatusBadge = "最新版",
                StatusBadgeBackground = "#E6F0FF",
                StatusBadgeForeground = "#3B82F6",
                ActionText = "启动",
                IsFavorite = false
            },
            new()
            {
                IconGlyph = "\uf080",
                IconBackground = "#FFF4D6",
                Title = "数据分析",
                Description = "数据可视化工具",
                StatusBadge = "最新版",
                StatusBadgeBackground = "#E6F0FF",
                StatusBadgeForeground = "#3B82F6",
                ActionText = "启动",
                IsFavorite = false
            }
        };

        Recommendations = new ObservableCollection<RecommendationModel>
        {
            new()
            {
                IconGlyph = "\uf03d",
                IconBackground = "#E8F2FF",
                Title = "视频编辑大师",
                Description = "专业视频剪辑与制作",
                Rating = 4.5,
                ReviewCount = 1243,
                Price = "¥199"
            },
            new()
            {
                IconGlyph = "\uf001",
                IconBackground = "#FFEFF5",
                Title = "音乐创作工坊",
                Description = "音乐制作与编辑工具",
                Rating = 4.0,
                ReviewCount = 867,
                Price = "¥299"
            }
        };

        RecentActivities = new ObservableCollection<ActivityItemModel>
        {
            new()
            {
                IconGlyph = "\uf19c",
                IconBackground = "#FFEFEF",
                Title = "年度会员特惠",
                Description = "购买年度会员，所有软件享折扣优惠",
                StatusText = "进行中",
                StatusBackground = "#FEE2E2",
                StatusForeground = "#EF4444"
            },
            new()
            {
                IconGlyph = "\uf06b",
                IconBackground = "#E8FFF4",
                Title = "新用户专享礼包",
                Description = "注册即可领取双份软件礼包",
                StatusText = "进行中",
                StatusBackground = "#DCFCE7",
                StatusForeground = "#16A34A"
            }
        };
    }

    private static void ReplaceCollection<T>(ObservableCollection<T> target, IReadOnlyList<T> source)
    {
        target.Clear();
        foreach (var item in source)
        {
            target.Add(item);
        }
    }
}
